package com.zhongge.model.dao.impl;

import com.zhongge.model.dao.ArticleDao;
import com.zhongge.model.entity.Article;
import com.zhongge.util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @ClassName ArticleDaoImpl
 * @Description TODO
 * @Author zhongge
 * @Version 1.0
 */
public class ArticleDaoImpl implements ArticleDao {
    /**
     * 根据ID查询文章
     * @param id 文章ID
     * @return 文章对象
     */
    @Override
    public Article findById(Integer id) {
        Connection cont = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;

        try {
            //第一步获取连接
            cont = JDBCUtil.getConnection();
            //查询sql==》
            String sql = "SELECT a.*, u.username, c.name as category_name " +
                    "FROM article a " +
                    "LEFT JOIN user u ON a.user_id = u.id " +
                    "LEFT JOIN category c ON a.category_id = c.id " +
                    "WHERE a.id = ?";
            pstmt = cont.prepareStatement(sql);
            pstmt.setInt(1, id);
            rs = pstmt.executeQuery();
            if (rs.next()) {
                Article article = new Article();
                article.setId(rs.getInt("id"));
                article.setTitle(rs.getString("title"));
                article.setContent(rs.getString("content"));
                article.setSummary(rs.getString("summary"));
                article.setUserId(rs.getInt("user_id"));
                article.setCategoryId(rs.getInt("category_id"));
                article.setViewCount(rs.getInt("view_count"));
                article.setIsPublished(rs.getBoolean("is_published"));
                article.setCreatedTime(rs.getTimestamp("create_time"));
                article.setUpdatedTime(rs.getTimestamp("update_time"));

                article.setUsername(rs.getString("username"));
                article.setCategoryName(rs.getString("category_name"));
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtil.close(cont, pstmt, rs);
        }

        return null;
    }

    @Override
    public List<Article> findAll(int page, int pageSize) {
        return List.of();
    }

    @Override
    public List<Article> findByUserId(Integer userId, int page, int pageSize) {
        return List.of();
    }

    @Override
    public List<Article> findByCategoryId(Integer categoryId, int page, int pageSize) {
        return List.of();
    }

    @Override
    public int insert(Article article) {
        return 0;
    }

    @Override
    public int update(Article article) {
        return 0;
    }

    @Override
    public int delete(Integer id) {
        return 0;
    }

    @Override
    public void increaseViewCount(Integer id) {

    }

    @Override
    public int getTotalCount() {
        return 0;
    }

    @Override
    public int getTotalCountByUserId(Integer userId) {
        return 0;
    }

    @Override
    public int getTotalCountByCategoryId(Integer categoryId) {
        return 0;
    }
}
